"""
收银台服务
"""

from jlpay.services.base_service import BaseService
from jlpay.model.cashier import (
    OrderQueryRequest, OrderQueryResponse,
    PreOrderRequest, PreOrderResponse,
    TradeCancelRequest, TradeCancelResponse,
    TradeRefundRequest, TradeRefundResponse
)


class CashierService(BaseService):
    """收银台服务类"""
    
    def __init__(self, config):
        """
        初始化收银台服务
        
        Args:
            config: OrgConfig实例
        """
        super().__init__(config)
    
    def get_api_url(self, request_class, api_path=None):
        """
        获取API URL
        
        Args:
            request_class: 请求类
            api_path: 接口路径
            
        Returns:
            完整的API URL
        """
        base_url = self.config.url
        
        if api_path:
            return f"{base_url}{api_path}"
        
        # 使用Request类中定义的API_PREFIX和API_URI
        api_prefix = getattr(request_class, 'API_PREFIX', '')
        api_uri = getattr(request_class, 'API_URI', '')
        
        if not api_uri:
            # 如果没有定义API_URI，使用默认路径
            return f"{base_url}/open/cashier/trans/trade"
        
        # 组合API路径
        path = api_prefix.lstrip('/') + '/' + api_uri.lstrip('/')
        return f"{base_url}/{path}"
    
    def query_order(self, request):
        """
        查询订单状态
        
        Args:
            request: OrderQueryRequest实例
            
        Returns:
            OrderQueryResponse实例
        """
        return self.post(request, OrderQueryResponse)
    
    def create_pre_order(self, request):
        """
        创建预下单
        
        Args:
            request: PreOrderRequest实例
            
        Returns:
            PreOrderResponse实例
        """
        return self.post(request, PreOrderResponse)
    
    def cancel_trade(self, request):
        """
        撤销交易
        
        Args:
            request: TradeCancelRequest实例
            
        Returns:
            TradeCancelResponse实例
        """
        return self.post(request, TradeCancelResponse)
    
    def refund_trade(self, request):
        """
        退款交易
        
        Args:
            request: TradeRefundRequest实例
            
        Returns:
            TradeRefundResponse实例
        """
        return self.post(request, TradeRefundResponse) 